package sitar.controllers;

import data.Task;
import oboe.core.SigSlot;
import php.Lib;
import php.Session;

/**
 * ...
 * @author Felipe Manga
 */

class Wall extends SigSlot
{

	public function new() 
	{
		super();
	}
	
	@slot function onPoll():Void
	{
		var rs = Main.db.query("
			SELECT
				tasks.id as id,
				tasks.issueTime as issueTime,
				tasks.startTime as startTime,
				tasks.endTime as endTime,
				tasks.projectId as projectId,
				tasks.roleId as roleId,
				tasks.userId as userId,
				tasks.toolId as toolId,
				tasks.message as message
			FROM tasks
			JOIN projects
			ON tasks.projectId = projects.id
			JOIN user_project
			ON user_project.projectId = projects.id
			AND user_project.userId = %0
			JOIN users
			ON users.id = %0
			WHERE tasks.roleId = user_project.roleId
			AND tasks.issueTime > users.lastMessagePollTime
			ORDER BY tasks.id ASC
			", 
			[ 
				Session.get("UID") 
			] 
		);
		
		var tasks = new Array<Task>();
		var fields = rs.getFieldsNames();
		tasks.push( new Task() );
		tasks[0].message = fields.join(", ");
		for ( row in rs )
		{
			var task = new Task();
			for ( field in fields )
			{
				if ( Reflect.hasField(task, field) )
				{
					Reflect.setField( task, field, Reflect.field( row, field ) );
				}
			}
			tasks.push( task );
		}
		
		this.shortSignal( "onClientMessage", ["notifyNewTask", [tasks]] );		
	}
}